/*
 * DiscoveryFragment.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.discovery;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;

import com.flyco.tablayout.SlidingTabLayout;
import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.data.response.DiscoveryMainTypeResponse;
import com.yidiandao.business.ui.adapter.DiscoveryItemPageAdapter;
import com.yidiandao.business.ui.adapter.DiscoveryPageAdapter;
import com.yidiandao.business.ui.main.discovery.page.DiscoveryPageFragment;
import com.yidiandao.business.ui.main.discovery.page.DiscoveryPagePresenter;
import com.yidiandao.business.ui.main.discovery.search.DiscoverySearchActivity;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 发现-View
 */
public class DiscoveryFragment extends SuperBaseFragment implements DiscoveryContract.View {

    public static final String TAG = DiscoveryFragment.class.getSimpleName();

    private ImageButton mIbtnSearch;

    private ViewPager mViewpager;

    private DiscoveryContract.Presenter mPresenter;

    private DiscoveryPageAdapter mPageAdapter;

    private SlidingTabLayout mTabLayout;

    /**
     * 标题的集合
     */
    private List<DiscoveryMainTypeResponse.CatTypeEntity> mTitleList = new ArrayList<>();

    private DiscoveryItemPageAdapter mDiscoveryItemPageAdapter;

    public static DiscoveryFragment getInstance() {
        return new DiscoveryFragment();
    }

    @Override
    protected void initViews() {
        mTabLayout = (SlidingTabLayout) mRootView.findViewById(R.id.tab_layout);
        mIbtnSearch = (ImageButton) mRootView.findViewById(R.id.ibtn_search);
        mViewpager = (ViewPager) mRootView.findViewById(R.id.viewpager);
    }

    private void initDataAndEvent() {
        initData();
        initEvent();
    }

    private void initEvent() {
        mIbtnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DiscoverySearchActivity.actionStart(mActivity);
            }
        });
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (null != savedInstanceState) {
            mPageAdapter = new DiscoveryPageAdapter(getChildFragmentManager());
            List<Fragment> fragments = getChildFragmentManager().getFragments();
            if (null != fragments) {
                for (Fragment fragment : fragments) {
                    if (fragment instanceof DiscoveryPageFragment) {
                        DiscoveryPageFragment pagerFragment = (DiscoveryPageFragment) fragment;
                        new DiscoveryPagePresenter(pagerFragment);
                        mPageAdapter.addFragment(pagerFragment);
                    }
                }
            }
        }
    }

    private void initData() {
        if (null == mDiscoveryItemPageAdapter) {
            mDiscoveryItemPageAdapter = new DiscoveryItemPageAdapter(getChildFragmentManager());
            for (int i = 0; i < mTitleList.size(); i++) {
                String catName = mTitleList.get(i).getTypeName();
                int typeId = mTitleList.get(i).getTypeId();

                // 添加一级页面
                DiscoveryPageFragment pagerFragment = DiscoveryPageFragment.getInstance(typeId);
                new DiscoveryPagePresenter(pagerFragment);
                mDiscoveryItemPageAdapter.addFragment(pagerFragment);
                mDiscoveryItemPageAdapter.addTitle(catName);
            }
        }

        //viewpager加载adapter
        mViewpager.setAdapter(mDiscoveryItemPageAdapter);
        //TabLayout加载viewpager
        mTabLayout.setViewPager(mViewpager);
        mTabLayout.onPageSelected(0);

        mViewpager.setOffscreenPageLimit(mTitleList.size());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_discovery;
    }

    @Override
    public void setPresenter(DiscoveryContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return TAG;
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }


    @Override
    public void setTitleList(List<DiscoveryMainTypeResponse.CatTypeEntity> catTypeBeen) {
        mTitleList.clear();
        mTitleList.addAll(catTypeBeen);
        initDataAndEvent();
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }
}
